<?php namespace App\Controllers\Admin;
/*
|=====================================================================
| Auth Controller
|=====================================================================
|
| This is the admin main class. Here is where we verify if the user is logged 
| and which is the information he can CRUD.
|
*/
class AuthController extends AdminController {
        
    // ---------------------------------------------------------------------        
    // Call login form.
    // ---------------------------------------------------------------------        
    public function getLogin() {
        $this->layout->content = \View::make('admin.login');
    }

    // ---------------------------------------------------------------------        
    // postLogin, is the function that receive the information to identify 
    // the users, and determine
    // ---------------------------------------------------------------------        
    public function postLogin() {

        // Basic requirements to login.
        $rules = array(
		    'email'  => 'required|max:50',
		    'password' => 'required'
		);

        // Email and Password password are the fields to identify an user.
	    $credentials = array(
	        'email'    => \Input::get('email'),
	        'password' => \Input::get('password')
	    );
	            
	    // Validate user data
		$validation = \Validator::make($credentials, $rules);
		if ($validation->fails()) {
		    // login fail!! redirecto to login form.
		    return \Redirect::to('admin/login')->withErrors($validation)->withInput()->with("message","Users or Password invalid!");
		}

		try
		{
			$user = \Sentry::authenticate($credentials, false);
			if (\Sentry::check())
			{   
			    // Also verify if the user is in the administration group.
				$admin = \Sentry::findGroupByName('Administration');
				if ($user->inGroup($admin))
			    {   
		            // login ok!!! redirect to dashboard.
                    return \Redirect::to('admin/dashboard');
                    exit(); 
			    }
			}
		}
		
		catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
		{
			return Redirect::to('admin/login')->withErrors(array('message' => 'Login field is required.'));
		}
		catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
		{
			return Redirect::to('admin/login')->withErrors(array('message' => 'Password field is required.'));
		}
		catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
		{
			return Redirect::to('admin/login')->withErrors(array('message' => 'Wrong password, try again.'));
		}
		catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
		{
			return Redirect::to('admin/login')->withErrors(array('message' => 'User was not found.'));
		}
		catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
		{
			return Redirect::to('admin/login')->withErrors(array('message' => 'User is not activated.'));
		}

		// The following is only required if throttle is enabled
		catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
		{
			return Redirect::to('admin/login')->withErrors(array('message' => 'User is suspended.'));
		}
		catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
		{
			return \Redirect::to('admin/login')->withErrors(array('message' => 'User is banned.'));
		}
    }
    
    // ---------------------------------------------------------------------        
    // Logout, is used to kill admin user session.
    // ---------------------------------------------------------------------        
    public function Logout() {
        \Sentry::logout();
        return \Redirect::to('admin/login')->with('message', 'Your are now logged out!');
    }

    // ---------------------------------------------------------------------        
    // getIndex, if the user is logged in go to dashboard other way go to login form
    // ---------------------------------------------------------------------            
    public function getIndex() {
        return \Redirect::to('admin/dashboard');
    }
}

